package com.example.coinhelper.Manage;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.NonNull;

import com.example.coinhelper.Bean.User;
import com.example.coinhelper.db.UserDBHelper;
import com.example.coinhelper.utils.MD5;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserManager {

    private SQLiteOpenHelper dbHelper;

    public UserManager(Context context) {
        dbHelper = new UserDBHelper(context);
    }

    // 添加用户
    public long addUser(@NonNull User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        user.setPassword(MD5.getMD5(user.getPassword()));
        values.put(UserDBHelper.COLUMN_USERNAME, user.getUsername());
        values.put(UserDBHelper.COLUMN_PASSWORD, user.getPassword());
        values.put(UserDBHelper.COLUMN_NAME, user.getName());
        values.put(UserDBHelper.COLUMN_ICON, user.getIcon());
        values.put(UserDBHelper.COLUMN_GENDER, user.getGender());
        values.put(UserDBHelper.COLUMN_MONEY, user.getMoney());
        values.put(UserDBHelper.COLUMN_SIGNATURE, user.getSignature());
        values.put(UserDBHelper.COLUMN_REGISTRATION_TIME, user.getRegistrationTime());

        long id = db.insert(UserDBHelper.TABLE_NAME, null, values);
        db.close();
        return id;
    }

    // 获取所有用户
    @SuppressLint("Range")
    public List<User> getAllUsers() {
        List<User> userList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(UserDBHelper.TABLE_NAME, null, null, null, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            do {
                User user = new User();
                // 从数据库中提取用户信息，并创建User对象
                user.setUsername(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_USERNAME)));
                user.setPassword(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_PASSWORD)));
                user.setName(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_NAME)));
                user.setIcon(cursor.getInt(cursor.getColumnIndex(UserDBHelper.COLUMN_ICON)));
                user.setGender(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_GENDER)));
                user.setRegistrationTime(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_REGISTRATION_TIME)));
                user.setSignature(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_SIGNATURE)));
                user.setMoney(cursor.getDouble(cursor.getColumnIndex(UserDBHelper.COLUMN_MONEY)));
                userList.add(user);
            } while (cursor.moveToNext());

            cursor.close();
        }
        db.close();
        return userList;
    }

    // 更新用户信息
    public int updateUser(@NonNull User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        // 更新用户信息
//        values.put(UserDBHelper.COLUMN_PASSWORD, user.getPassword());
//        values.put(UserDBHelper.COLUMN_NAME, user.getName());
//        values.put(UserDBHelper.COLUMN_ICON, user.getIcon());
//        values.put(UserDBHelper.COLUMN_GENDER, user.getGender());
//        values.put(UserDBHelper.COLUMN_MONEY, user.getMoney());
//        values.put(UserDBHelper.COLUMN_SIGNATURE, user.getSignature());
        values.put(UserDBHelper.COLUMN_REGISTRATION_TIME, user.getRegistrationTime());

        int rowsAffected = db.update(UserDBHelper.TABLE_NAME, values, UserDBHelper.COLUMN_USERNAME + " = ?",
                new String[]{user.getUsername()});
        db.close();
        return rowsAffected;
    }

    // 删除用户
    public void deleteUser(String username) {

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.execSQL("PRAGMA foreign_keys = ON;");
        db.delete(UserDBHelper.TABLE_NAME, UserDBHelper.COLUMN_USERNAME + " = ?", new String[]{username});
        db.close();
    }
    @SuppressLint("Range")
    public User login(@NonNull User user) {
        user.setPassword(MD5.getMD5(user.getPassword()));
        // 获取可读数据库连接
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        // 需要查询的列
        String[] projection = {
                UserDBHelper.COLUMN_USERNAME,
                UserDBHelper.COLUMN_PASSWORD,
                UserDBHelper.COLUMN_REGISTRATION_TIME,
        };

        // 查询条件：WHERE username = ? AND password = ?
        String selection = UserDBHelper.COLUMN_USERNAME + " = ? AND " + UserDBHelper.COLUMN_PASSWORD + " = ?";
        String[] selectionArgs = {user.getUsername(), user.getPassword()};

        // 执行查询
        Cursor cursor = db.query(
                UserDBHelper.TABLE_NAME,    // 表名
                projection,                 // 查询的列
                selection,                  // 查询条件
                selectionArgs,              // 查询条件参数
                null,                       // GROUP BY
                null,                       // HAVING
                null                        // ORDER BY
        );

        User loggedInUser = null;

        // 如果查询结果非空并且移动到第一行
        if (cursor != null && cursor.moveToFirst()) {
            // 创建 User 对象并设置相关信息
            loggedInUser = new User();
            loggedInUser.setUsername(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_USERNAME)));
            loggedInUser.setPassword(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_PASSWORD)));
//            loggedInUser.setName(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_NAME)));
//            loggedInUser.setIcon(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_ICON)));
//            loggedInUser.setGender(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_GENDER)));
            loggedInUser.setRegistrationTime(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_REGISTRATION_TIME)));
//            loggedInUser.setSignature(cursor.getString(cursor.getColumnIndex(UserDBHelper.COLUMN_SIGNATURE)));
        }
        // 关闭游标和数据库连接
        if (cursor != null) {
            cursor.close();
        }
        db.close();
        // 返回登录用户对象
        return loggedInUser;
    }
    public int updateUserPassword(@NonNull User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        user.setPassword(MD5.getMD5(user.getPassword()));
        // 更新用户信息
        values.put(UserDBHelper.COLUMN_PASSWORD, user.getPassword());
//        values.put(UserDBHelper.COLUMN_NAME, user.getName());
//        values.put(UserDBHelper.COLUMN_ICON, user.getIcon());
//        values.put(UserDBHelper.COLUMN_GENDER, user.getGender());
//        values.put(UserDBHelper.COLUMN_MONEY, user.getMoney());
//        values.put(UserDBHelper.COLUMN_SIGNATURE, user.getSignature());
//        values.put(UserDBHelper.COLUMN_REGISTRATION_TIME, user.getRegistrationTime());

        int rowsAffected = db.update(UserDBHelper.TABLE_NAME, values, UserDBHelper.COLUMN_USERNAME + " = ?",
                new String[]{user.getUsername()});
        db.close();
        return rowsAffected;
    }

}
